#ifndef     __DRAW_TANK_TASK_H
#define     __DRAW_TANK_TASK_H

#include "../rtos/thread.h"
#include "../rtos/message_queue.h"
#include "../rtos/event_flags.h"

namespace heat_task {

class DrawTankTask : public rtos::Thread<DrawTankTask, 1024, osPriorityHigh2> {

public:
    enum class State : uint8_t {
        IDLE = 0,
        WORK = 1,
        FAIL = 2,
        TIMEOUT = 3,
        STOP = 4,
    };

    enum class TankState : uint8_t {
        TANK1 = 0,
        TANK2 = 1,
        FAIL = 2,
    };

    static constexpr uint32_t FLAG_STOP = 0x01;

    uint8_t state() { return static_cast<uint8_t>(m_state); }
    
    void init_start();

    void run();

    void stop() {
        m_flags.clr_flags(FLAG_STOP);
        m_mq.post(State::STOP);
        m_flags.wait_any_flags(FLAG_STOP);
    }

    void idle() {
        m_tank_state = TankState::TANK1;
        m_mq.post(State::IDLE);
    }

private:
    bool wait(uint32_t timeout = osWaitForever) { return osOK == m_mq.poll(m_state, timeout); }

    void do_idle();
    void do_work();
    void do_stop();
    void do_fail();

    State m_state = State::IDLE;
    rtos::lazy::MessageQueue<State, 2> m_mq;
    TankState m_tank_state = TankState::TANK1;
    rtos::lazy::EventFlags m_flags;
};

extern DrawTankTask drawTankTask;

}

#endif
